/*
 *  Copyright (C) 2009  Peter Kist & Jan Ripke
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "Consumer.h"
#include "Manager.h"

#include <Util/Logger.h>
#include <Core/Interface/Gfx.h>
#include <Modules/Gfx/Ogre/Gui/MenuManager.h>
#include <Core/Interface/Input.h>

#include <Core/Mediator/Event.h>

namespace galaxy {
    namespace gfx {
        namespace ogre3d {

            const char * Consumer::c_ConsumerType = "gfx.ogre.Consumer";

            Consumer::Consumer(Manager& manager)
            {
                this->m_Manager = &manager;
            }

            Consumer::~Consumer(void)
            {
            }

            void Consumer::handleEvent (mediator::Event& event) {
                
                LOG_TRACE (std::string ("gfx thread: event received: "));
                handleSingleEvent (event);
            }

            bool Consumer::handleQueuedEvent (mediator::Event& event) {
                handleSingleEvent (event);
                return true;
            }

            void Consumer::handleSingleEvent (mediator::Event& event) {
                switch (event.category()) {
                    case mediator::CATEGORY_INPUT: 
                        {
                            if (event.type() == MODULE_TYPE_INPUT_KEY 
                                && event.subtype() == MODULE_SUBTYPE_INPUT_KEYRELEASE)
                            {
                                handleKeyRelease (event);
                            }
                            if (event.type() == MODULE_TYPE_INPUT_MOUSE) {
                                handleMouseEvent (event);
                            }
                            break;
                        }
                    case mediator::CATEGORY_GFX:
                        {
                            if (event.type() == MODULE_TYPE_GFX_MENU_COMMAND) {
                                handleMenuEvent(event);
                            }
                            break;
                        }
                }
            }

            void Consumer::handleKeyRelease (mediator::Event& event) {
                input::KeyEvent& keyEvent = (input::KeyEvent&) event;
                //m_Manager->getMenuManager().handleKeyEvent (keyEvent);
            }

            void Consumer::handleMouseEvent (mediator::Event& event) {
                input::MouseEvent& mouseEvent = (input::MouseEvent&) event;
                m_Manager->getMenuManager()->handleMouseEvent (mouseEvent);
            }

            void Consumer::handleMenuEvent (mediator::Event& event)
            {
                gfx::MenuEvent& menuEvent = (gfx::MenuEvent&) event;

                gfx::Menu::Command command = (gfx::Menu::Command) menuEvent.subtype ();
                switch (command) {
                    case gfx::Menu::GFX_MENU_SHOW:
                        {
                            MenuManagerPtr& menuManager = m_Manager->getMenuManager();
                            if (menuManager->changeMenu (menuEvent.m_MenuName) == false) {
                                LOG_ERROR (std::string ("show menu ") + menuEvent.m_MenuName + "failed");
                            }
                            break;
                        }
                    case gfx::Menu::GFX_MENU_HIDE:
                        {
                            MenuManagerPtr& menuManager = m_Manager->getMenuManager();
                            if (menuManager->hideMenu () == false) {
                                LOG_ERROR (std::string ("hide menu ") + menuEvent.m_MenuName + "failed");
                            }
                            break;
                        }
                }
            }

        } // namespace ogre3d
    } // namespace gfx
} // namespace galaxy
